package com.application.chiettinhdienluc.ui;

import java.io.File;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.application.chiettinhdienluc.R;
import com.application.chiettinhdienluc.app.MainApplication;
import com.application.chiettinhdienluc.data.GlobalData;
import com.application.chiettinhdienluc.model.ChietTinhModel;
import com.application.chiettinhdienluc.utils.Utils;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingSherlockFragmentActivity;

public class BaseActivity extends SlidingSherlockFragmentActivity implements
		OnClickListener {
	private TextView tvMenuLoginUser;
	private RelativeLayout layoutupdateaccount;
	private RelativeLayout layoutfirst;
	//private RelativeLayout layoutsecond;
	private RelativeLayout layoutthird;

	private RelativeLayout layoutfourth;
	private RelativeLayout layoutfifth;
	private RelativeLayout layoutseventh;
	private RelativeLayout layouteighth;
	private RelativeLayout layoutsixth;

	private int mTitleRes;

	public BaseActivity(int titleRes) {
		mTitleRes = titleRes;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setTitle(mTitleRes);

		// set the Behind View
		setBehindContentView(R.layout.menu_frame);

		// customize the SlidingMenu
		SlidingMenu sm = getSlidingMenu();
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setFadeDegree(0.35f);
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		
		tvMenuLoginUser = (TextView) findViewById(R.id.tvMenuLoginUser);
		String name = MainApplication.getNguoiDung() != null ? MainApplication.getNguoiDung().get_TenNguoiDung() : "";
		tvMenuLoginUser.setText(name);
		
		
		layoutupdateaccount = (RelativeLayout)findViewById(R.id.layoutLoginUser);
		layoutupdateaccount.setOnClickListener(this);
		
		layoutfirst = (RelativeLayout) findViewById(R.id.layoutfirst);
		layoutfirst.setOnClickListener(this);
		
		//layoutsecond = (RelativeLayout)findViewById(R.id.layoutsecond);
		//layoutsecond.setOnClickListener(this);
		
		layoutthird = (RelativeLayout) findViewById(R.id.layoutthird);
		layoutthird.setOnClickListener(this);

		layoutfourth = (RelativeLayout) findViewById(R.id.layoutfourth);
		layoutfourth.setOnClickListener(this);

		layoutfifth = (RelativeLayout) findViewById(R.id.layoutfifth);
		layoutfifth.setOnClickListener(this);

		layoutseventh = (RelativeLayout) findViewById(R.id.layoutseventh);
		layoutseventh.setOnClickListener(this);

		layouteighth = (RelativeLayout) findViewById(R.id.layouteighth);
		layouteighth.setOnClickListener(this);

		layoutsixth = (RelativeLayout) findViewById(R.id.layoutsixth);
		layoutsixth.setOnClickListener(this);
		// ActionBar mActionBar = getSupportActionBar();
		// if (mActionBar!=null) {
		// mActionBar.hide();
		// }
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	// @Override
	// protected void onPause() {
	// TODO Auto-generated method stub
	// Utils.setLockByPassCode(4, GlobalData.usePassCode, this, false);
	// super.onPause();
	// }

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	public void onClick(View v) {
		
		if (v == layoutupdateaccount) {
			Intent localIntent = new Intent(
					BaseActivity.this,
					com.application.chiettinhdienluc.ui.AccountSettingActivity.class);
			localIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(localIntent);
		}
		else if(v == layoutfirst)
		{
			try {
				
			
             // TODO Auto-generated method stub
				GlobalData.SelectedNhanCongList.clear();
				GlobalData.SelectedVatTuList.clear();
				GlobalData.HinhAnhList.clear();
				
				GlobalData.globalChietTinh = new ChietTinhModel();
                KhaosatInfoFragment.needRefreshEmptyValueKhaosatInfo = true;
                ResultInfoFragment.needRefreshEmptyValueResultInfo = true;
                
                Intent localIntent = new Intent(BaseActivity.this, com.application.chiettinhdienluc.ui.MainTabFragmentActivity.class);
                localIntent.putExtra(MainTabFragmentActivity.STR_EXTRA_IMODE, MainTabFragmentActivity.MODE_NEW_INPUT);
                startActivityForResult(localIntent, 1000);
                //getSlidingMenu().showContent();
            } catch (Exception e) {
                // TODO: handle exception
            }
		}	
		else if (v == layoutthird) {
			if (!isActivityAlreadyStarted(com.application.chiettinhdienluc.ui.DanhMucVatTuActivity.class
					.getName())) {
				Intent localIntent = new Intent(
						BaseActivity.this,
						com.application.chiettinhdienluc.ui.DanhMucVatTuActivity.class);
				localIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(localIntent);
			}
		}
		else if (v == layoutfourth) {

			if (!isActivityAlreadyStarted(com.application.chiettinhdienluc.ui.DanhMucNhanCongActivity.class
					.getName())) {

				Intent localIntent = new Intent(
						BaseActivity.this,
						com.application.chiettinhdienluc.ui.DanhMucNhanCongActivity.class);
				localIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(localIntent);
			}

		}
		else if (v == layoutfifth) {

			if (!isActivityAlreadyStarted(com.application.chiettinhdienluc.ui.DanhMucNhanVienActivity.class
					.getName())) {
				Intent localIntent = new Intent(
						BaseActivity.this,
						com.application.chiettinhdienluc.ui.DanhMucNhanVienActivity.class);
				localIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(localIntent);
			}

		}
		else if (v == layoutseventh) {

			if (!isActivityAlreadyStarted(com.application.chiettinhdienluc.ui.SettingsActivity.class
					.getName())) {
				Intent localIntent = new Intent(
						BaseActivity.this,
						com.application.chiettinhdienluc.ui.SettingsActivity.class);
				localIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(localIntent);
			}

		}
		else if (v == layouteighth) {

			MainApplication.logOut();

			Intent localIntent = new Intent(
					BaseActivity.this,
					com.application.chiettinhdienluc.ui.LoginActivity.class);
			localIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(localIntent);
			finish();
		}
		else if (v == layoutsixth) {

			if (!isActivityAlreadyStarted(com.application.chiettinhdienluc.ui.DanhMucTramActivity.class
					.getName())) {
				Intent localIntent = new Intent(
						BaseActivity.this,
						com.application.chiettinhdienluc.ui.DanhMucTramActivity.class);
				localIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(localIntent);
			}

		}
		
		

	}

	private Boolean isActivityAlreadyStarted(String activityName) {
		ActivityManager am = (ActivityManager) MainApplication.getAppContext()
				.getSystemService(Activity.ACTIVITY_SERVICE);
		// String packageName =
		// am.getRunningTasks(1).get(0).topActivity.getPackageName();
		String className = am.getRunningTasks(1).get(0).topActivity
				.getClassName();

		Log.i("BaseActivity", className);

		return className.equalsIgnoreCase(activityName);

	}

}
